package com.ym.domain.common.exception;

import com.ym.domain.common.base.Result;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.format.DateTimeParseException;

@ControllerAdvice
public class SimpleExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Result handleException(MethodArgumentNotValidException e) {
        return Result.fail(e.getBindingResult().getFieldError().getDefaultMessage(), false);
    }

    @ResponseBody
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        return Result.fail(e.getMessage(), false);
    }

    @ResponseBody
    @ExceptionHandler(value = DateTimeParseException.class)
    public Result handleDateTimeParseException(DateTimeParseException e) {
        return Result.fail(e.getMessage(), false);
    }

    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public Result handleBusinessException(BusinessException e) {
        return Result.fail(e.getMessage(), false);
    }


}
